<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column
        prop="name"
        label="课程名称"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="teacher"
        label="课程老师"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="peopleNumber"
        label="参与学生人数"
      ></el-table-column>
      <el-table-column prop="info" label="课程简介"></el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template slot-scope="scope">
          <el-button @click="lookClick(scope.$index)" type="text" size="small"
            >查看</el-button
          >
          <el-button type="text" @click="updateClick(scope.$index)" size="small"
            >编辑</el-button
          >
          <el-button type="text" @click="delCourse(scope.$index)" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="position: absolute; top: 20px; right: 30px; z-index: 1000">
      <el-button type="button" @click="dialogFormVisibleAdd = true"
        >添加课程</el-button
      >
    </div>
    <el-dialog title="课程详情" :visible.sync="dialogFormVisible">
      <el-form :model="form" :disabled="true">
        <el-form-item label="课程名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="参与老师：" :label-width="formLabelWidth">
          <el-input v-model="form.teacher" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="参与人数：" :label-width="formLabelWidth">
          {{ form.peopleNumber }}
        </el-form-item>
        <el-form-item label="课程简介" :label-width="formLabelWidth">
          <el-input v-model="form.info" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog title="修改详情" :visible.sync="dialogFormVisibleU">
      <el-form :model="form" ref="updateForm" :rules="rules">
        <el-form-item
          label="课程名称"
          prop="name"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="参与老师：" :label-width="formLabelWidth">
          <el-select v-model="form.teacher" placeholder="请选择参与老师">
            <el-option
              v-for="(teacher, index) in taechersA"
              :key="index"
              :label="teacher.name"
              :value="teacher.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参与人数：" :label-width="formLabelWidth">
          {{ form.peopleNumber }}
        </el-form-item>
        <el-form-item
          label="课程简介"
          prop="info"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.info" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleU = false">取 消</el-button>
        <el-button type="primary" @click="preservationClick">保 存</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加课程" :visible.sync="dialogFormVisibleAdd">
      <el-form ref="addForm" :model="form" :rules="rules">
        <el-form-item
          label="课程名称"
          prop="name"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="参与老师：" :label-width="formLabelWidth">
          <el-select v-model="form.teacher" placeholder="请选择参与老师">
            <el-option
              v-for="(teacher, index) in taechersA"
              :key="index"
              :label="teacher.name"
              :value="teacher.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参与人数：" :label-width="formLabelWidth">
          {{ form.peopleNumber }}
        </el-form-item>
        <el-form-item
          label="课程简介"
          prop="info"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.info" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="addClick">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { courses, taechers } from "../../../public/schoolData.js";
export default {
  name: "courseList",
  data() {
    return {
      tableData: courses,
      dialogFormVisible: false,
      dialogFormVisibleU: false,
      dialogFormVisibleAdd: false,
      form: {
        id: 0, //id
        name: "", //课程名称
        teacher: "", //参与的老师
        peopleNumber: 0, //参与人数
        info: "", //课程的内容
      },
      formLabelWidth: "120px",
      taechersA: taechers,
      handleIndex: 0,
      rules: {
        name: [
          { required: true, message: "课程名称不可为空", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
        info: [
          { required: true, message: "内容不可为空", trigger: "blur" },
          {
            min: 5,
            max: 30,
            message: "长度在 5 到 30 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    dialogFormVisible(newVal) {
      if (!newVal)
        (this.form.name = ""),
          (this.form.teacher = ""),
          (this.form.info = ""),
          (this.form.peopleNumber = 0);
    },
    dialogFormVisibleU(newVal) {
      if (!newVal) {
        (this.form.name = ""),
          (this.form.teacher = ""),
          (this.form.info = ""),
          (this.form.peopleNumber = 0);
      }
    },
  },
  methods: {
    lookClick(index) {
      this.handleIndex = index;
      this.dialogFormVisible = true;
      this.form.id = courses[index].id;
      this.form.name = courses[index].name;
      this.form.teacher =
        courses[index].teacher == "" ? "无" : courses[index].teacher;
      this.form.peopleNumber = courses[index].peopleNumber;
      this.form.info = courses[index].info == "" ? "无" : courses[index].info;
    },
    updateClick(index) {
      this.handleIndex = index;
      this.dialogFormVisibleU = true;
      this.form.id = courses[index].id;
      this.form.name = courses[index].name;
      this.form.teacher = courses[index].teacher;
      this.form.peopleNumber = courses[index].peopleNumber;
      this.form.info = courses[index].info;
    },
    addClick() {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          let teacherA = taechers.find((data) => {
            return data.name == this.form.teacher;
          });

          let cId = 1;
          if (courses.length > 0) {
            cId = courses[courses.length - 1].id + 1;
          } else {
            cId = 1;
          }
          let addObj = {
            id: cId, //id
            name: this.form.name, //课程名称
            teacher: this.form.teacher, //参与的老师
            peopleNumber: 0, //参与人数
            info: this.form.info, //课程的内容
            teacherId: teacherA.id,
          };
          courses.push(addObj);
          this.dialogFormVisibleAdd = false;
          (this.form.name = ""),
            (this.form.teacher = ""),
            (this.form.info = "");
        }
      });
    },
    preservationClick() {
      this.$refs["updateForm"].validate((valid) => {
        if (valid) {
          courses[this.handleIndex].name = this.form.name;
          courses[this.handleIndex].teacher = this.form.teacher;
          courses[this.handleIndex].info = this.form.info;
          let teacherA = taechers.find((data) => {
            return data.name == this.form.teacher;
          });
          courses[this.handleIndex].teacherId = teacherA.id;
          console.log(courses);
          this.dialogFormVisibleU = false;
        }
      });
    },
    delCourse(index) {
      courses.splice(index, 1);
    },
  },
};
</script>
<style scoped>
.el-input {
  width: 300px;
}
</style>